package com.example.springboot.mqtt.listener;

import com.example.springboot.mqtt.factory.MqttFactory;
import com.example.springboot.mqtt.serivice.IMqttService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttMessageListener;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.nio.charset.StandardCharsets;

@Slf4j
public class MqttListener implements IMqttMessageListener {
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) {
        IMqttService mqttService = MqttFactory.getMqttFactory(topic);
        if(mqttService!=null){
            mqttService.analysisMessage(mqttMessage.toString());
        }else{
            log.error("接收topic异常:{}",topic);
        }
        log.info("topic:{},message:{}",topic,mqttMessage.toString());
    }
}
